package com.freeman.cement.util;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {
    //前端入库日期转换
    public static Date getDate(Date date) throws ParseException {
        //date转换--由如“Wed Mar 31 00:00:00 CST 2021”转为2021-5-31
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String strDate= sdf.format(date);
        Date newDate =sdf.parse(strDate);
        java.sql.Date resultDate = new java.sql.Date(newDate.getTime());
        return resultDate;
    }
    //时间戳转换---十位时间戳-->精确到秒
    public static String getUnixToDateTime(long second) {
        String seconds = String.valueOf(second);
        String format = "yyyy年MM月dd日";
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()) {format = "yyyy年MM月dd日";}
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));

    }
    //时间戳转换---十位时间戳-->精确到秒
    public static String getUnixToDateTimeLAYUI(long second) {
        String seconds = String.valueOf(second);
        String format = "yyyy-MM-dd HH:mm:ss";
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()) {format = "yyyy-MM-dd HH:mm:ss";}
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));

    }

    public static String getUnixToDateTimeChinese(long second) {
        String seconds = String.valueOf(second);
        String format = "yyyy年MM月dd日 HH:mm:ss";
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()) {format = "yyyy年MM月dd日 HH:mm:ss";}
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));

    }
    public static String getUnixToMDTime(long second) {
        String seconds = String.valueOf(second);
        String format = "MM月dd日";
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()) {format = "MM月dd日";}
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));

    }
    //时间戳转换---精确到秒-->十位时间戳
    public static String getDateToTimeUnix(String datetime) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return String.valueOf(sdf.parse(datetime).getTime() / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    //时间戳转换---精确到秒-->十三位时间戳
    public static String getDateToTimeUnixMM(String datetime) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return String.valueOf(sdf.parse(datetime).getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    //时间戳转换---十位时间戳-->精确到秒
    public static String getUnixToDateTimeMM(String str_seconds) {
        String format = "yyyy-MM-dd HH:mm:ss";
        if(str_seconds == null || str_seconds.isEmpty() || str_seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){ format = "yyyy-MM-dd HH:mm:ss";}
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(str_seconds)));

    }

    //时间戳转换---十位时间戳-->精确到秒
    public static String getUnixToDateTime(String str_seconds) {
        String format = "yyyy年MM月dd日";
        if(str_seconds == null || str_seconds.isEmpty() || "null".equals(str_seconds)){
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.parseLong(str_seconds)));

    }


    /**
     * 日期路径 即年/月/日 如2018/08/08
     * qqb
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

}

